define(
["ui/QueryViewSyst"],
function(QueryViewSyst){
    return {
        getName: function() {
            return "COMP - QUERY VIEW"
        },

        create: function() {
            var _POI;
            var _CLASS = this;

            function _onPicked() {
                //Get osmInfo's content
                var content = _POI.getOsmInfo().getContent();

                //Hide all viewer components
                _queryViewSyst.hideAllViews();

                //Set title
                _queryViewSyst.setFeatureTitle(
                    content.name,
                    content.amenity
                );

                //Show list of tags
                _queryViewSyst.showTags(content);

                //Set Indoor if necessary
                //Get indoorMgr
                var indoorMgr = _getIndoorMgr();
                if(indoorMgr) {
                    _queryViewSyst.setIndoorOptions(
                        indoorMgr.getCurFloor(),
                        indoorMgr.getMaxFloor(),
                        indoorMgr.isVisible(),
                        _onCurFloorChanged, //Callback when current floor changed
                        _onIndoorToggled //Callback when indoor toggles on / off
                    )
                }
            }

            function _onCurFloorChanged(curFloor) {
                _getIndoorMgr().setCurFloor(curFloor);
            }

            function _onIndoorToggled() {
                var indoorMgr = _getIndoorMgr();
                _setIndoorVisibility(indoorMgr, !indoorMgr.isVisible());
            }

            function _getIndoorMgr() {
                return _POI.getIndoorMgr() ?
                    _POI.getIndoorMgr() :
                    _POI.getParentIndoorMgr();
            }

            function _setIndoorVisibility(indoorMgr, visible) {
                indoorMgr.setVisible(visible);
                indoorMgr.getPOI().getRenderer().setVisible(!visible);
            }

            function _camGoToPOI(pos) {
                var pos = _POI.getRenderer().getCenterPos();
                var cam = _queryViewSyst.getCam();
                var forward = cam.forward;

                vec3.set(pos, cam.target);
                vec3.subtract(pos, forward, cam.pos);

                cam.update();
            } 

            function _select() {
                //Go to POI's position
                _camGoToPOI();

                //Hide parent's shell
                var parentIndoorMgr = _POI.getParentIndoorMgr();
                if(parentIndoorMgr) {
                    _setIndoorVisibility(parentIndoorMgr, true);

                    //Set current floor as my floor
                    var curFloor = parentIndoorMgr.getFloorOf(_POI);
                    parentIndoorMgr.setCurFloor(curFloor);
                }

                //Make sure I'm visible
                var indoorMgr = _POI.getIndoorMgr();
                if(indoorMgr) {
                    _setIndoorVisibility(indoorMgr, false);
                }

                //Behave as if I'm picked
                _onPicked();
            }

            var _self = {
                init: function(systemMgr, POI) {
                    //Assign members
                    _POI = POI;
                    _queryViewSyst = systemMgr.getSystem(QueryViewSyst);

                    //Register myself to system
                    _queryViewSyst.addComp(_self);

                    //Initialize picking reaction
                    _POI.getRenderer().addPickListener(_onPicked);

                    //Return myself
                    return _self;
                },

                getClass: function() { return _CLASS; },

                getOsmId: function() { return _POI.getOsmInfo().getId(); },

                select: _select,

                destroy: function() {
                    //TODO unreg from system, unreg listener from render unit
                    throw "QueryViewSystComp.js - destroy() - not yet implemented";
                }
            };

            return _self;
        }
    };
});